const express = require('express'), //express 框架 
	wechat = require('./wechat/wechat'),
	config = require('./wechatConfig');//引入配置文件
var proxyMiddleware = require('http-proxy-middleware')
var app = express();//实例express框架
app.use(express.static('dist'));
var wechatApp = new wechat(config); //实例wechat 模块

wechatApp.setMenu()

let proxyTable = {
	'/API': {
		// 测试环境
		target: 'https://game.royal-lucifer.top',  // 接口域名
		changeOrigin: true,  //是否跨域
		pathRewrite: {
			'^/API': '/API'  //需要rewrite重写的,
		},
		headers:{}
	},
	'/wechat': {
		// 测试环境
		target: 'https://api.weixin.qq.com/sns',  // 接口域名
		changeOrigin: true,  //是否跨域
		pathRewrite: {
			'^/wechat': '/'  //需要rewrite重写的,
		},
		logLevel:'debug'
	}
}

Object.keys(proxyTable).forEach(function (context) {
	var options = proxyTable[context]
	if (typeof options === 'string') {
		options = { target: options }
	}
	app.use(proxyMiddleware(options.filter || context, options))
})

//用于处理所有进入 3000 端口 get 的连接请求
app.get('/', function (req, res) {
	wechatApp.auth(req, res);
});

//用于处理所有进入 3000 端口 post 的连接请求
app.post('/', function (req, res) {
	wechatApp.handleMsg(req, res);
});

//用于请求获取 access_token
app.get('/getAccessToken', function (req, res) {
	wechatApp.getAccessToken().then(function (data) {
		console.log('getAccessToken', data)
		res.send(data);
	});
});

//监听3000端口
app.listen(80, () => {
	console.log('listen 127.0.0.1:80')
});